<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="评论用户" prop="userId">
        <!-- <el-input
          v-model="queryParams.userId"
          placeholder="请输入评论用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.userId" placeholder="请选择评论用户">
          <el-option
            v-for="item in commentList2"
            :key="item.userId"
            :label="item.user.userName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="作品ID" prop="vlogId">
        <!-- <el-input
          v-model="queryParams.vlogId"
          placeholder="请输入作品ID"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.vlogId" placeholder="请选择作品ID">
          <el-option
            v-for="item in commentList3"
            :key="item.userId"
            :label="item.vlogId"
            :value="item.vlogId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="isAudit">
        <el-select v-model="queryParams.isAudit" placeholder="请选择审核状态">
          <el-option
            v-for="item in [
              { value: '0', label: '未审核' },
              { value: '1', label: '通过' },
              { value: '2', label: '不通过' },
            ]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:comment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:comment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:comment:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:comment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="评论用户" align="center" >
        <template slot-scope="scope">{{ scope.row.user.userName }}</template>
      </el-table-column>
      <el-table-column label="评论内容" align="center" prop="content" />
      <!-- <el-table-column label="评论图片 多个用逗号隔开," align="center" prop="images" /> -->
      <!-- <el-table-column label="定位地点" align="center" prop="addr" /> -->
      <el-table-column label="作品ID" align="center" prop="vlogId" />
      <el-table-column label="点赞量" align="center" prop="likeVolume" />
      <!-- <el-table-column label="上级ID" align="center" prop="pid" /> -->
      <el-table-column label="审核" align="center" prop="isAudit" >
        <template slot-scope="scope">
          <el-tag type="danger" size="mini" v-if="scope.row.isAudit == 0"
            >未审核</el-tag
          >
          <el-tag type="success" size="mini" v-if="scope.row.isAudit == 1"
            >通过</el-tag
          >
          <el-tag type="warning" size="mini" v-if="scope.row.isAudit == 2"
            >不通过</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" :icon="scope.row.isAudit==0? 'el-icon-edit':'el-icon-view'" @click="handleUpdate(scope.row)"
						v-hasPermi="['system:comment:edit']">{{scope.row.isAudit==0 ? '审核':'查看'}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:comment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改作品评论信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="评论用户ID">
          <el-input v-model="user.userName" placeholder="请输入评论用户ID" disabled />
        </el-form-item>
        <el-form-item label="评论内容">
          <!-- <editor v-model="form.content" :min-height="192" /> -->
          <el-input
            v-model="form.content"
            type="textarea"
            placeholder="请输入内容"
            disabled
          />
        </el-form-item>
        <el-form-item label="评论图片" prop="images">
          <template v-if='form.images'>
            <image-preview
                v-for="(item, index) in form.images.split(',')"
                :src="api_host + item"
                :width="100"
                :height="100"
              />
          </template>
          <p v-else><el-tag>无评论图片</el-tag></p>
        </el-form-item>
        <!-- <el-form-item label="定位地点" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入定位地点" />
        </el-form-item> -->
        <el-form-item label="作品ID">
          <el-input v-model="form.vlogId" placeholder="请输入作品ID" disabled />
        </el-form-item>
        <el-form-item label="审核结果">
					<el-radio-group v-model="form.isAudit">
						<el-radio label='1'>通过</el-radio>
						<el-radio label='2'>未通过</el-radio>
					</el-radio-group>
				</el-form-item>
        <!-- <el-form-item label="点赞量" prop="likeVolume">
          <el-input v-model="form.likeVolume" placeholder="请输入点赞量" />
        </el-form-item>
        <el-form-item label="上级ID" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入上级ID" />
        </el-form-item>
        <el-form-item label="审核  0未审核  1通过 2不通过" prop="isAudit">
          <el-input v-model="form.isAudit" placeholder="请输入审核  0未审核  1通过 2不通过" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer" v-if='isShow==0'>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComment, getComment, delComment, addComment, updateComment } from "@/api/system/vlogcomment";

export default {
  name: "Comment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 作品评论信息表格数据
      commentList: [],
      commentList2:[],
      commentList3:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        vlogId: null,
        isAudit: null
      },
      // 表单参数
      form: {},
      user:{},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "评论用户ID不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "评论内容不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        vlogId: [
          { required: true, message: "作品ID不能为空", trigger: "blur" }
        ],
        likeVolume: [
          { required: true, message: "点赞量不能为空", trigger: "blur" }
        ],
        isAudit: [
          { required: true, message: "审核  0未审核  1通过 2不通过不能为空", trigger: "blur" }
        ]
      },
      isShow:0
    };
  },
  created() {
    this.getList();
    this.getList2()
  },
  methods: {
    /** 查询作品评论信息列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then(response => {
        this.commentList = response.rows;
        // this.commentList2 = this.distinct(this.commentList,'userId')
        // this.commentList3 = this.distinct(this.commentList,'vlogId')
        this.total = response.total;
        this.loading = false;
      });
    },
    getList2(){
      this.loading = true;
      listComment().then(response => {
        this.commentList2 = this.distinct(this.commentList,'userId')
        this.commentList3 = this.distinct(this.commentList,'vlogId')
        this.loading = false;
      });
    },
    //去重
    distinct(arr, key) {
      let newobj = {}; 
      arr = arr .reduce((preVal, curVal) => {
          newobj[curVal[key]] ? '' : newobj[curVal[key]] = preVal.push(curVal); 
          return preVal 
      }, []);
      return arr;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        content: null,
        images: null,
        createTime: null,
        addr: null,
        vlogId: null,
        likeVolume: null,
        pid: null,
        isAudit: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加作品评论信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getComment(id).then(response => {
        this.form = response.data;
        this.user = response.data.user
        this.form.isAudit = response.data.isAudit.toString()
        this.isShow = response.data.isAudit
        this.open = true;
        this.title = "审核作品评论";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateComment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除作品评论信息编号为"' + ids + '"的数据项？').then(function() {
        return delComment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/comment/export', {
        ...this.queryParams
      }, `comment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
